package com.alibaba.order.service.impl;

import com.alibaba.basic.service.impl.BaseServiceImpl;
import com.alibaba.order.domain.OrderPetAcquisition;
import com.alibaba.order.dto.ConfirmDto;
import com.alibaba.order.mapper.OrderPetAcquisitionMapper;
import com.alibaba.order.service.IOrderPetAcquisitionService;
import com.alibaba.pet.domain.Pet;
import com.alibaba.pet.domain.SearchMasterMsg;
import com.alibaba.pet.mapper.PetMapper;
import com.alibaba.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2023-03-17
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private OrderPetAcquisitionMapper orderMapper;

    @Autowired
    private SearchMasterMsgMapper msgMapper;
    @Autowired
    private PetMapper petMapper;
    @Override
    public void cannel(Long id) {
        // 1收购订单状态改为取消
        OrderPetAcquisition order = new OrderPetAcquisition();
        order.setId(id);
        order.setState(-1);
        orderMapper.update(order);
        // 2修改寻主消息状态
        OrderPetAcquisition byId = orderMapper.finById(id);
        SearchMasterMsg msg = new SearchMasterMsg();
        msg.setId(byId.getMsgId());
        msg.setState(1);
        msg.setShopId(null);
        msgMapper.updateStateId(msg);
        // 3删除宠物
        petMapper.delete(order.getPetId());
    }

    /**
     * 确认订单
     * @param dto
     */
    @Override
    public void confirm(ConfirmDto dto) {
        // 参数校验
        // todo 要根据订单状态 只有待处理的时候 才可以修改
        // 修改订单的状态为 已支付
        OrderPetAcquisition order = orderMapper.finById(dto.getId());
        order.setState(1);
        order.setPrice(dto.getMoney());
        order.setPayType(dto.getPayType());
        orderMapper.update(order);

        // 修改msg状态为已完成
        SearchMasterMsg msg = new SearchMasterMsg();
        msg.setId(order.getMsgId());
        msg.setState(4);
        msgMapper.update(msg);

        // 修改pet的价格
        Pet pet = new Pet();
        pet.setId(order.getPetId());
        pet.setCostprice(dto.getMoney());
        petMapper.update(pet);
    }
}
